<template>
  <div class="app-container">
    <el-row style="margin-bottom: 0">
      <!-- 查询表单 -->
      <el-col :span="22">
        <el-form :inline="true" size="medium">
          <el-form-item>
            <el-select v-model="searchObj.paid" clearable placeholder="支付状态" style="width:120px">
              <el-option value="" label="全部"/>
              <el-option value="1" label="已支付"/>
              <el-option value="0" label="未支付"/>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-input v-model="searchObj.orderNo" clearable placeholder="订单号" style="width:190px"/>
          </el-form-item>
          <el-form-item>
            <el-input v-model="searchObj.courseTitle" clearable placeholder="课程名称" style="width:150px"/>
          </el-form-item>
          <el-form-item>
            <el-input v-model="searchObj.teacherName" clearable placeholder="讲师姓名" style="width:120px"/>
          </el-form-item>
          <el-form-item>
            <el-input v-model="searchObj.name" clearable placeholder="会员用户名" style="width:150px"/>
          </el-form-item>
          <el-form-item>
            <el-input v-model="searchObj.phone" clearable placeholder="会员手机号" style="width:150px"/>
          </el-form-item>
          <el-form-item>
            <el-button-group>
              <el-button type="primary" icon="el-icon-search" @click="fetchData()">查询</el-button>
              <el-button type="warning" icon="el-icon-refresh-right" @click="resetData()">重置</el-button>
            </el-button-group>
          </el-form-item>
        </el-form>
      </el-col>
      <!-- 工具栏 -->
      <el-col :span="2" style="text-align: right">
        <div style="margin-top: 4px">
          <el-button round type="success" icon="el-icon-download" size="small" @click="output()">导出</el-button>
        </div>
      </el-col>
    </el-row>
    <!-- 表格 -->
    <el-row>
      <el-col :span="24">
        <el-table
          :data="list"
          :header-cell-style="{'text-align':'center'}"
          fit
          stripe
          tooltip-effect="light"
          v-loading="loading"
          highlight-current-row
          element-lodaing-text="数据加载中"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" min-width="2%" align="center"/>
          <el-table-column type="index" label="#" min-width="2%" align="center">
            <template slot-scope="scope">
              {{ (page - 1) * limit + scope.$index + 1 }}
            </template>
          </el-table-column>
          <el-table-column prop="status" label="支付状态" min-width="8%" align="center">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.paid" effect="dark" type="primary" size="medium">
                已支付
              </el-tag>
              <el-tag v-else effect="dark" type="info" size="medium">
                未支付
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="orderNo" label="订单号" min-width="13%" align="center"/>
          <el-table-column prop="courseTitle" label="课程标题" min-width="20%" align="center" show-overflow-tooltip/>
          <el-table-column prop="teacherName" label="讲师姓名" min-width="8%" align="center"/>
          <el-table-column prop="name" label="会员用户名" min-width="10%" align="center"/>
          <el-table-column prop="phone" label="会员手机号" min-width="11%" align="center">
            <template slot-scope="scope">
              <i class="el-icon-mobile"/>
              {{ scope.row.phone }}
            </template>
          </el-table-column>
          <el-table-column prop="totalFee" label="订单金额 (元)" min-width="10%" align="center"/>
          <el-table-column prop="gmtCreate" label="下单时间" sortable min-width="16%" align="center">
            <template slot-scope="scope">
              <i class="el-icon-time"></i>
              {{ scope.row.gmtCreate }}
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
    <!-- 分页组件 -->
    <el-row>
      <el-col :span="24">
        <el-pagination
          :current-page="page"
          :total="total"
          :page-size="limit"
          :page-sizes="[5, 10, 15, 20, 50, 100]"
          style="padding: 30px 0; text-align: center;"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="changePageSize"
          @current-change="changeCurrentPage"
        />
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { orderApi } from '@/api'

export default {
  data() {
    return {
      loading: true, // 加载遮罩
      list: [], // 订单列表
      total: 0, // 总记录数
      page: 1, // 页码
      limit: 10, // 每页记录数
      searchObj: {
        subjectId: ''// 解决查询表单无法选中二级科目
      }, // 查询条件
      selection: [], // 选中的行
      USER_API: process.env.VUE_APP_WEB_API
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    // 获取订单列表
    fetchData() {
      // 开始加载
      this.loading = true
      // 调用api
      orderApi.queryByPage(this.page, this.limit, this.searchObj).then(response => {
        this.list = response.data.items
        this.total = response.data.total
        // 结束加载
        this.loading = false
      })
    },
    output() {
      if (this.selection.length === 0) {
        this.$message.warning('请选择要导出的记录')
        return
      }
      this.$confirm('将会导出这些记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        console.log(this.selection)
        // 调用api
        return orderApi.output(this.selection)
      }).then((response) => {
          this.$notify.success({
            title: response.message + ' 文件目录',
            message: response.data.filePath,
            duration: 8000
          })
        }
      ).catch(error => {
        if (error === 'cancel') {
          this.$message.info('取消导出')
        }
      })
    },
    // 每页记录数改变时
    changePageSize(size) {
      this.limit = size
      this.fetchData()
    },
    // 页码改变时
    changeCurrentPage(page) {
      this.page = page
      this.fetchData()
    },
    // 重置表单
    resetData() {
      this.searchObj = {}
      this.fetchData()
    },
    // 复选框选项发生变化时
    handleSelectionChange(selection) {
      this.selection = selection
    }
  }
}
</script>

<style lang="scss">
@import "~@/assets/css/table_head_align.scss";

.cover-img {
  width: 213px;
  height: 119px;
  border-radius: 10px;
  margin: 0 auto -6px;
}

.info-label {
  margin-right: 10px;
  color: #909399;
}

.price {
  color: #FFC233;
  font-weight: bold;
  font-family: "Microsoft YaHei";
}

.free {
  color: #42B983;
}

.el-button + .el-button {
  margin-left: 5px;
}
</style>
